package com.db;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.Marker;

/**
 * Simple logger implementation
 * 
 * @author a.troitskaya
 */
public class MyLogger implements Logger {
	private String name;
	private LevelLogger level;
	private Appender app;
	private final String propertyFile = "/MyLoggerProperties.properties";

	public MyLogger(String name) {
		this(name, LevelLogger.INFO);
	}
	public MyLogger(String name, LevelLogger level) {
		this.name = name;
		this.level = level;
		InputStream in = this.getClass().getResourceAsStream(propertyFile);
		Properties prop = new Properties();
		try {
			prop.load(in);
			if ("console".equals(prop.getProperty("myLogger.appender"))) {
				app = ConsoleAppender.getAppender();
			}
			else {
				if ("file".equals(prop.getProperty("myLogger.appender"))) {
					app = AsyncFileAppender.getAppender();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void setLevel(LevelLogger level) {
		this.level = level;
	}
	public Appender getAppender() {
		return this.app;
	}
	public boolean isDebugEnabled() {
		if ((level.equals(LevelLogger.DEBUG)) || (level.equals(LevelLogger.INFO))) {
			return true;
		} else
			return false;
	}

	public boolean isWarnEnabled() {
		if ((level.equals(LevelLogger.DEBUG)) || (level.equals(LevelLogger.INFO)) || (level.equals(LevelLogger.WARN))) {
			return true;
		} else
			return false;
	}

	public boolean isInfoEnabled() {
		if ((level.equals(LevelLogger.INFO))) {
			return true;
		} else
			return false;
	}
	
	public boolean isErrorEnabled() {
		return true;
	}
	
	public boolean isTraceEnabled() {
		return false;
	}

	public String getName() {
		return name;
	}

	public void debug(String message) {
		if (isDebugEnabled())
			app.append("DEBUG" + " " + message);
	}
	
	public void debug(String format, Object arg) {
		if (isDebugEnabled()) {
			String message = format.replaceFirst("\\{\\}", arg.toString());
			app.append("DEBUG"+" "+message);
		}

	}

	public void debug(String format, Object arg1, Object arg2) {
		if (isDebugEnabled()) {
			String message = format.replaceFirst("\\{\\}", arg1.toString());
			message = message.replaceFirst("\\{\\}", arg2.toString());
			app.append("DEBUG"+" "+message);
		}
	}
	
	public void debug(String format, Object... arguments) {
		if (isDebugEnabled()) {
			String message = format.replaceFirst("\\{\\}", arguments[0].toString());
			for (int i = 1; i < arguments.length; i++) {
				message = message.replaceFirst("\\{\\}", arguments[i].toString());
			}
			app.append("DEBUG"+" "+message);
		}
	}
	
	public void debug(String msg, Throwable t) {
		if (isDebugEnabled()) {
			app.append("DEBUG"+" "+msg+" "+t.getMessage());
		}

	}

	public boolean isDebugEnabled(Marker marker) {
		return false;
	}

	public void debug(Marker marker, String msg) {
		// TODO Auto-generated method stub
	}

	public void debug(Marker marker, String format, Object arg) {
		// TODO Auto-generated method stub
	}

	public void debug(Marker marker, String format, Object arg1, Object arg2) {
		// TODO Auto-generated method stub
	}

	public void debug(Marker marker, String format, Object... arguments) {
		// TODO Auto-generated method stub
	}

	public void debug(Marker marker, String msg, Throwable t) {
		// TODO Auto-generated method stub
	}

	public void trace(String message) {
		if (isTraceEnabled())
			app.append("TRACE" + " " + message);
	}

	public void trace(String format, Object arg) {
		if (isTraceEnabled()) {
			String message = format.replaceFirst("\\{\\}", arg.toString());
			app.append("TRACE"+" "+message);
		}

	}

	public void trace(String format, Object arg1, Object arg2) {
		if (isTraceEnabled()) {
			String message = format.replaceFirst("\\{\\}", arg1.toString());
			message = message.replaceFirst("\\{\\}", arg2.toString());
			app.append("TRACE"+" "+message);
		}
	}
	
	public void trace(String format, Object... arguments) {
		if (isTraceEnabled()) {
			String message = format.replaceFirst("\\{\\}", arguments[0].toString());
			for (int i = 1; i < arguments.length; i++) {
				message = message.replaceFirst("\\{\\}", arguments[i].toString());
			}
			app.append("TRACE"+" "+message);
		}

	}
	
	public void trace(String msg, Throwable t) {
		if (isTraceEnabled()) {
			app.append("TRACE"+" "+msg+" "+t.getMessage());
		}

	}

	public boolean isTraceEnabled(Marker marker) {
		// TODO Auto-generated method stub
		return false;
	}

	public void trace(Marker marker, String msg) {
		// TODO Auto-generated method stub

	}

	public void trace(Marker marker, String format, Object arg) {
		// TODO Auto-generated method stub

	}

	public void trace(Marker marker, String format, Object arg1, Object arg2) {
		// TODO Auto-generated method stub

	}

	public void trace(Marker marker, String format, Object... argArray) {
		// TODO Auto-generated method stub

	}

	public void trace(Marker marker, String msg, Throwable t) {
		// TODO Auto-generated method stub

	}


	public void info(String message) {
		if (isInfoEnabled())
			app.append("INFO" + " " + message);
	}

	public void info(String format, Object arg) {
		if (isInfoEnabled()) {
			String message = format.replaceFirst("\\{\\}", arg.toString());
			app.append("INFO"+" "+message);
		}

	}

	public void info(String format, Object arg1, Object arg2) {
		if (isInfoEnabled()) {
			String message = format.replaceFirst("\\{\\}", arg1.toString());
			message = message.replaceFirst("\\{\\}", arg2.toString());
			app.append("INFO"+" "+message);
		}
	}
	
	public void info(String format, Object... arguments) {
		if (isInfoEnabled()) {
			String message = format.replaceFirst("\\{\\}", arguments[0].toString());
			for (int i = 1; i < arguments.length; i++) {
				message = message.replaceFirst("\\{\\}", arguments[i].toString());
			}
			app.append("INFO"+" "+message);
		}

	}
	
	public void info(String msg, Throwable t) {
		if (isInfoEnabled()) {
			app.append("INFO"+" "+msg+" "+t.getMessage());
		}

	}


	public boolean isInfoEnabled(Marker marker) {
		return false;
	}
	
	public void info(Marker marker, String msg) {
		// TODO Auto-generated method stub

	}

	public void info(Marker marker, String format, Object arg) {
		// TODO Auto-generated method stub

	}

	public void info(Marker marker, String format, Object arg1, Object arg2) {
		// TODO Auto-generated method stub

	}

	public void info(Marker marker, String format, Object... arguments) {
		// TODO Auto-generated method stub

	}

	public void info(Marker marker, String msg, Throwable t) {
		// TODO Auto-generated method stub

	}


	public void warn(String message) {
		if (isWarnEnabled())
			app.append("WARN" + " " + message);
	}

	public void warn(String format, Object arg) {
		if (isWarnEnabled()) {
			String message = format.replaceFirst("\\{\\}", arg.toString());
			app.append("WARN"+" "+message);
		}

	}

	public void warn(String format, Object arg1, Object arg2) {
		if (isWarnEnabled()) {
			String message = format.replaceFirst("\\{\\}", arg1.toString());
			message = message.replaceFirst("\\{\\}", arg2.toString());
			app.append("WARN"+" "+message);
		}
	}
	
	public void warn(String format, Object... arguments) {
		if (isWarnEnabled()) {
			String message = format.replaceFirst("\\{\\}", arguments[0].toString());
			for (int i = 1; i < arguments.length; i++) {
				message = message.replaceFirst("\\{\\}", arguments[i].toString());
			}
			app.append("WARN"+" "+message);
		}

	}
	
	public void warn(String msg, Throwable t) {
		if (isWarnEnabled()) {
			app.append("WARN"+" "+msg+" "+t.getMessage());
		}

	}
	
	public boolean isWarnEnabled(Marker marker) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void warn(Marker marker, String msg) {
		// TODO Auto-generated method stub

	}

	public void warn(Marker marker, String format, Object arg) {
		// TODO Auto-generated method stub

	}

	public void warn(Marker marker, String format, Object arg1, Object arg2) {
		// TODO Auto-generated method stub

	}

	public void warn(Marker marker, String format, Object... arguments) {
		// TODO Auto-generated method stub

	}

	public void warn(Marker marker, String msg, Throwable t) {
		// TODO Auto-generated method stub

	}

	public void error(String message) {
		app.append("ERROR" + " " + message);
	}

	public void error(String format, Object arg) {
		String message = format.replaceFirst("\\{\\}", arg.toString());
		app.append("ERROR"+" "+message);
	}

	public void error(String format, Object arg1, Object arg2) {
		String message = format.replaceFirst("\\{\\}", arg1.toString());
		message = message.replaceFirst("\\{\\}", arg2.toString());
		app.append("ERROR"+" "+message);
	}
	
	public void error(String format, Object... arguments) {
		String message = format.replaceFirst("\\{\\}", arguments[0].toString());
		for (int i = 1; i < arguments.length; i++) {
			message = message.replaceFirst("\\{\\}", arguments[i].toString());
		}
		app.append("ERROR"+" "+message);
	}
	
	public void error(String msg, Throwable t) {
		app.append("ERROR"+" "+msg+" "+t.getMessage());
	}

	public boolean isErrorEnabled(Marker marker) {
		// TODO Auto-generated method stub
		return false;
	}

	public void error(Marker marker, String msg) {
		// TODO Auto-generated method stub

	}

	public void error(Marker marker, String format, Object arg) {
		// TODO Auto-generated method stub

	}

	public void error(Marker marker, String format, Object arg1, Object arg2) {
		// TODO Auto-generated method stub

	}

	public void error(Marker marker, String format, Object... arguments) {
		// TODO Auto-generated method stub

	}

	public void error(Marker marker, String msg, Throwable t) {
		// TODO Auto-generated method stub

	}

}
